//
//  PlistIO.m
//  rrdSJ
//
//  Created by yangaichun on 16/3/17.
//  Copyright © 2016年 com.weiba66. All rights reserved.
//

#import "PlistIO.h"

@implementation PlistIO

+(NSArray *)arrayFromMainBundlePlist:(NSString *)fileName{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:fileName ofType:@"plist"];
    return  [[NSArray alloc]initWithContentsOfFile:path];
}

+(NSArray *)ArrayFromDocumentPlist:(NSString *)fileName{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:fileName];   //获取路径
    return  [[NSArray alloc]initWithContentsOfFile:filename];
}



+(NSDictionary *)dictionaryFromMainBundlePlist:(NSString *)fileName{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:fileName ofType:@"plist"];
    return  [[NSDictionary alloc]initWithContentsOfFile:path];
}

+(NSDictionary *)dictionaryFromDocumentPlist:(NSString *)fileName{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:fileName];   //获取路径
    return  [[NSDictionary alloc]initWithContentsOfFile:filename];
}


+ (BOOL)writeArray:(NSArray *)array ToFile:(NSString *)fileName;{

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:fileName];   //获取路径
    return [array writeToFile:filename atomically:NO];
}

+ (BOOL)writeDictionary:(NSDictionary *)dictionary ToFile:(NSString *)fileName;{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:fileName];   //获取路径
    return [dictionary writeToFile:filename atomically:NO];
}



+ (void)deletFile:(NSString *)fileName;{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:fileName];   //获取路径
    
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    if([fileManager fileExistsAtPath:filename]){
        NSError *error;
        [fileManager removeItemAtPath:filename error:&error];
    }
}

@end
